//
// Created by chen on 2023/6/23.
//
#include "reservationManagement.h"

ReservationManagement::ReservationManagement() {

}

ReservationManagement::~ReservationManagement() {

}

void ReservationManagement::showMenu() {
    cout << "****************************************" << endl;
    cout << "***********欢迎使用机房预约系统*************" << endl;
    cout << "***********   1、学生登录    *************" << endl;
    cout << "***********   2、教师登录    *************" << endl;
    cout << "***********   3、管理员登录   *************" << endl;
    cout << "***********   0、退出系统    *************" << endl;
    cout << "****************************************" << endl;\
    cout << "请选择:";
}

void ReservationManagement::exitSystem() {
    cout << "退出机房预约系统，欢迎下次使用" << endl;
    exit(0);
}

void studentHandler(string id, string name){
    Student st(id, name);
    bool isLoop = true;
    while (isLoop){
        int choose = -1;
        st.showMenu();
        cin >> choose;
        switch (choose) {
            case 1:  // 申请预约
                st.applyReservation();
                break;
            case 2:  // 查看所有预约
                st.showAllReservation();
                break;
            case 3:  // 查看自身预约
                st.showSelfReservation();
                break;
            case 4:  // 取消预约
                st.cancelReservation();
                break;
            case 0:  // 注销登录
                st.logOut();
                isLoop = false;
                break;
            default:
                cout << "输入有误，重新输入" << endl;
                break;
        }
    }
}

void ReservationManagement::studentLogin() {
    if(at.studentPasswd.size() == 0){
        cout << "暂无学生用户，请联系管理员添加" << endl;
        return;
    }
    string id;
    string name;
    string passwd;
    cout << "请输入学生ID:";
    cin >> id;
    cout << "请输入学生姓名:";
    cin >> name;
    cout << "请输入密码:";
    cin >> passwd;
    map<string, map<string, string>>::iterator it = at.studentPasswd.find(id);
    if(it == at.studentPasswd.end()){
        cout << "用户ID不存在" << endl;
        return;
    } else{
        map<string, string>::iterator it2 = it->second.find(name);
        if(it2 != it->second.end()){
            cout << "登录成功！" << endl;
            studentHandler(id, name);
        } else{
            cout << "用户名或密码错误，请确认!" << endl;
            return;
        }
    }
}

void teacherHandler(string id, string name){
    Teacher tc(id, name);
    bool isLoop = true;
    while (isLoop){
        int choose = -1;
        tc.showMenu();
        cin >> choose;
        switch (choose) {
            case 1:  // 查看所有预约
                tc.viewAllReservation();
                break;
            case 2:  // 审核预约
                tc.checkReservation();
                break;
            case 0:  // 注销登录
                tc.logOut();
                isLoop = false;
                break;
            default:
                cout << "输入有误，重新输入" << endl;
                break;
        }
    }
}

void ReservationManagement::teacherLogin() {
    if(at.teacherPasswd.size() == 0){
        cout << "暂无教师用户，请联系管理员添加" << endl;
        return;
    }
    string id;
    string name;
    string passwd;
    cout << "请输入教师工号:";
    cin >> id;
    cout << "请输入教师姓名:";
    cin >> name;
    cout << "请输入密码:";
    cin >> passwd;
    map<string, map<string, string>>::iterator it = at.teacherPasswd.find(id);
    if(it == at.teacherPasswd.end()){
        cout << "用户ID不存在" << endl;
        return;
    } else{
        map<string, string>::iterator it2 = it->second.find(name);
        if(it2 != it->second.end()){
            cout << "登录成功！" << endl;
            teacherHandler(id, name);
        } else{
            cout << "用户名或密码错误，请确认!" << endl;
            return;
        }
    }
}

void managerHandler(string name){
    Manager ma(name);
    bool isLoop = true;
    while (isLoop){
        int choose = -1;
        ma.showMenu();
        cin >> choose;
        switch (choose) {
            case 1:  // 添加账号
                ma.addAccount();
                break;
            case 2:  // 查看账号信息
                ma.viewAccountInfo();
                break;
            case 3:  // 查看机房信息
                ma.viewRoomInfo();
                break;
            case 4:  // 清空预约
                ma.clearAllReservation();
                break;
            case 0:  // 注销登录
                ma.logOut();
                isLoop = false;
                break;
            default:
                cout << "输入有误，重新输入" << endl;
                break;
        }
    }
}

void ReservationManagement::managerLogin() {
    string name;
    string passwd;
    cout << "请输入管理员姓名:";
    cin >> name;
    cout << "请输入密码:";
    cin >> passwd;
    map<string, string>::iterator it = at.managerPasswd.find(name);
    if(it == at.managerPasswd.end()){
        cout << "用户不存在" << endl;
        return;
    } else{
        if(it->second == passwd){
            cout << "登录成功！" << endl;
            managerHandler(name);
        } else{
            cout << "密码错误，请确认!" << endl;
            return;
        }
    }
}

